Skip to content

指定区域识字 - Ocr

函数简介

识别窗口范围内的文字,x1、y1、x2、y2 传 0, 0, 0, 0 为窗口整个客户区。

接口名称

Ocr

DLL调用

long Ocr(long ola, int x1, int y1, int x2, int y2);

参数说明

参数名类型说明
ola长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
x1整数型区域左上角X坐标
y1整数型区域左上角Y坐标
x2整数型区域右下角X坐标
y2整数型区域右下角Y坐标

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 0,0,0,0 表示绑定窗口整个客户区
string text = ola.Ocr(0, 0, 0, 0);
// text 为识别到的文字,多行以换行分隔
csharp
using OLAPlug;

var ola = new OLAPlugServer();
// 0,0,0,0 表示绑定窗口整个客户区
string text = ola.Ocr(0, 0, 0, 0);
// text 为识别到的文字,多行以换行分隔
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
# 0,0,0,0 表示绑定窗口整个客户区
text = ola.Ocr(0, 0, 0, 0)
# text 为识别到的文字,多行以换行分隔
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
// 0,0,0,0 表示绑定窗口整个客户区
String text = ola.Ocr(0, 0, 0, 0);
// text 为识别到的文字,多行以换行分隔
cpp
var ola = com("OlaPlug.OlaSoft")
// 0,0,0,0 表示绑定窗口整个客户区
var text = ola.Ocr(0, 0, 0, 0)
// text 为识别到的文字,多行以换行分隔
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
' 0,0,0,0 表示绑定窗口整个客户区
text = ola.Ocr(0, 0, 0, 0)
' text 为识别到的文字,多行以换行分隔
text
.局部变量 ola, OLAPlug
ola.创建 ()
' 0,0,0,0 表示绑定窗口整个客户区
text = ola.Ocr(0, 0, 0, 0)
' text 为识别到的文字,多行以换行分隔
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
// 0,0,0,0 表示绑定窗口整个客户区
var text = ola.Ocr(0, 0, 0, 0);
// text 为识别到的文字,多行以换行分隔
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
// 0,0,0,0 表示绑定窗口整个客户区
文本型 text = ola.Ocr(0, 0, 0, 0)
// text 为识别到的文字,多行以换行分隔
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 0,0,0,0 表示绑定窗口整个客户区
string text = ola.Ocr(0, 0, 0, 0);
// text 为识别到的文字,多行以换行分隔

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
// 0,0,0,0 表示绑定窗口整个客户区
long textPtr = Ocr(instance, 0, 0, 0, 0);
if (textPtr != 0) {
    char text[512] = {0};
    GetStringFromPtr(textPtr, text, sizeof(text));
    FreeStringPtr(textPtr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();

long instance = CreateCOLAPlugInterFace();
// 0,0,0,0 表示绑定窗口整个客户区
long textPtr = Ocr(instance, 0, 0, 0, 0);
if (textPtr != 0) {
    StringBuilder text = new StringBuilder(GetStringSize(textPtr) + 1);
    GetStringFromPtr(textPtr, text, text.Capacity);
    FreeStringPtr(textPtr);
    string textStr = text.ToString();
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
# 0,0,0,0 表示绑定窗口整个客户区
textPtr = Ocr(instance, 0, 0, 0, 0)
if textPtr:
    buf = create_string_buffer(512)
    ola.GetStringFromPtr(textPtr, buf, 512)
    ola.FreeStringPtr(textPtr)
    text = buf.value.decode("utf-8")

返回值

字符串指针地址,返回识别到的字符串。返回的字符串指针需调用 FreeStringPtr 释放内存。

注意事项

  • 字体比较特殊或者背景复杂识别不准确的,建议用图像识别来处理